<?php
namespace app\team\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;

use app\team\model\Team;

class Login extends Controller
{
    public function login(){
        return view();
    }

    public function checkLogin(){
        if(Request::instance()->isAjax()){
            $post = Request::instance()->post();
            $post = $post['data'];
            //检查验证码

            if(captcha_check($post['captcha'])){
                //检查账号密码
                //加密方式
                $hasher = new \PasswordHash(8, false);
                $check = Db::name('team_admin')->where("t_admin_tel = '{$post['t_admin_name']}'")->find();
                if($check){
                    if($check['is_use']==0){
                        return ['code'=>'账号已被禁用,请联系管理员'];
                    }
                    if ($hasher->CheckPassword($post['password'], $check['password'])){

                        //查询景区信息
                        $team = Team::getTourById($check['team_id']);
                        Session::set('team',$team);
                        Session::set('team_id',$check['team_id']);
                        Session::set('admin_relname',$check['admin_rename']);
                        Session::set('t_admin_id',$check['t_admin_id']);
                        Session::set('t_admin_pid',$check['pid']);
                        return ['code'=>0];
                    }else{
                        return ['code'=>'密码错误'];
                    }

                }else{
                    return ['code'=>'账号不存在'];
                }
            }else{
                return ['code'=>'验证码错误'];
            }
        }
    }
}
